package cse.cf.common;

import cse.cf.database.Comment;
import cse.cf.database.Log;
import cse.cf.database.Profile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Converter {

    static final SimpleDateFormat sdf;

    static {
        sdf = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
    }

    public static Date unixTimestamp2Date(String timestamp) {
        long t = Long.parseLong(timestamp);
        t = (t) * 1000;
        return new Date(t);
    }

    public static Date unixTimestamp2Date(Long timestamp) {
        return new Date(timestamp * 1000);
    }

    public static String unixTimestamp2DateSimpleFormat(String timestamp) {
        return date2SimpleFormat(unixTimestamp2Date(timestamp));
    }

    public static String unixTimestamp2DateSimpleFormat(Long timestamp) {
        return date2SimpleFormat(unixTimestamp2Date(timestamp));
    }

    public static String date2SimpleFormat(Date date) {
        return sdf.format(date);
    }

    public static Comment fbComment2dbComment(com.restfb.types.Comment fbc) {
        Comment c = new Comment();
        c.setCommentId(fbc.getId());
        c.setOwnerProfileId(Long.parseLong(fbc.getFrom().getId()));
        c.setFromProfileId(c.getOwnerProfileId());
        c.setToProfileId(c.getOwnerProfileId());
        c.setTime(fbc.getCreatedTime().getTime() / 1000);
        c.setContent(fbc.getMessage());
        return c;
    }

    public static Comment fbPost2dbComment(com.restfb.types.Post fbp) {
        Comment c = new Comment();
        c.setCommentId(fbp.getId());
        c.setOwnerProfileId(Long.parseLong(fbp.getFrom().getId()));
        c.setFromProfileId(c.getOwnerProfileId());
        c.setToProfileId(c.getOwnerProfileId());
        c.setTime(fbp.getCreatedTime().getTime() / 1000);
        c.setContent(fbp.getMessage());
        return c;
    }

    public static Profile fbUser2dbProfile(com.restfb.types.User user) {
        Profile p = new Profile();
        p.setProfileId(Long.parseLong(user.getId()));
        p.setName(user.getName());
        p.setUsername(user.getUsername());
        p.setLink(user.getLink());
        p.setGender(user.getGender());
        return p;
    }
}
